<?php
$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);
?>
		<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_persiapan, #tgl_prod, #tgl_selesai").datetimepicker({
					changeMonth: true,
				    changeYear: true,
				    showOtherMonths: true,
					dateFormat:'yy-mm-dd',
					timeFormat: 'hh:mm:ss',
			        hourGrid: 8,
			        minuteGrid: 10,		
				});
				
				$(':checkbox[readonly=readonly]').click(function(){
					$(this).removeAttr('id');	
					$(this).removeAttr('class');						
            		return false;
        		});

        		$(":radio[readonly=readonly], select[readonly=readonly]").click(function(){	
					$(this).attr('disabled', true);
            		return false;
        		});

        		$(":input[readonly=readonly]").click(function(){	
        			var name = $(this).attr('name');
        			//console.log(name)
        			if( name!='jumlah'  && name!='type' &&  name!='jumlah_per_karton' ){
        				$(this).removeAttr('id');	
						$(this).removeAttr('class');						
	            		return false;
        			}
					
        		});

				$("#id_formula").change(function(){
					$.ajax({						
						url:'getKodeBB.php',
						dataType: "html",
						type:'POST',
						data:'id_formula='+$(this).val(),
						success: function(data) {
							var obj = $.parseJSON(data);						
							if(data=="[]"){
								$("#jumlah").val('');
								$("#jumlah_per_karton").val('');		
								$("#type").val('');	
								$("#id_formula2").val('');									
							}else{							
								$("#jumlah").val(obj[0].jumlah);
								$("#jumlah_per_karton").val(obj[0].jumlah_per_karton);
								$("#type").val(obj[0].types);		
								$("#id_formula2").val(obj[0].id_formula);								
							}
						}
					})
				})
				
			});
		</script>
<?php
if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Produksi</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=produksi" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="produksi">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="kode_prod" <?php echo ($field=="kode_prod")?"selected":"";?>>Kode Produksi</option>
									<option value="id_formula" <?php echo ($field=="id_formula")?"selected":"";?>>Kode Formula</option>
									<option value="tgl_persiapan" <?php echo ($field=="tgl_persiapan")?"selected":"";?>>Tgl. Persiapan</option>
									<option value="tgl_prod" <?php echo ($field=="tgl_prod")?"selected":"";?>>Tgl. Produksi</option>
									<option value="tgl_selesai" <?php echo ($field=="tgl_selesai")?"selected":"";?>>Tgl. Selesai</option>																		
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th>Kode Produksi</th>
									<th>Kode Formula</th>
									<th>Jumlah Produksi</th>
									<th>Sisa Produksi</th>
									<th>Tgl. Persiapan</th>
									<th>Status Persiapan</th>
									<th>Tgl. Produksi</th>
									<th>Status Produksi</th>
									<th>Tgl. Selesai</th>
									<th>Status Selesai</th>									
									<th class="action" colspan="2"><a href="?pages=produksi&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;

							if($field=='id_formula'){
								$sqlFormula = mysql_query("SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'");
								$onoFormula = mysql_num_rows($sqlFormula);
								//echo "SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'";
								while ($dataFormula = mysql_fetch_array($sqlFormula)) {
									$qx .= $dataFormula['id_formula'].',';
								}

								if($onoFormula)
									$q2 = substr($qx, 0, -1);
								else
									$q2 = '999999999999999999999';

							}
							
							$addQuery='';


							if(!empty($field)){
								if($field=='id_formula')
									$addQuery .= " WHERE $field IN ($q2) ";
								else
									$addQuery .= " WHERE $field LIKE '%$q2%' ";
							}

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlProd = mysql_query("SELECT * FROM tb_produksi $addQuery ORDER BY id_produksi DESC limit $posisi, $batas");
							//echo "SELECT * FROM tb_produksi $addQuery ORDER BY id_produksi DESC limit $posisi, $batas";
							$no=$posisi+1;
							while($dataProd = mysql_fetch_array($sqlProd)){			
								$dataFormula = mysql_fetch_array(mysql_query("SELECT kode FROM tb_formula WHERE id_formula='$dataProd[id_formula]' LIMIT 1"));		
								//echo "SELECT kode FROM tb_formula WHERE id='$dataProd[id_formula]' LIMIT 1";

								$statusPersiapan = ($dataProd['status_persiapan']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";
								$statusProduksi = ($dataProd['status_prod']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";
								$statusSelesai = ($dataProd['status_selesai']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";								
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td class="center"><?php echo $dataProd['kode_prod'];?></td>
									<td class="center"><?php echo $dataFormula['kode'];?></td>
									<td class="center"><?php echo $dataProd['jumlah'];?> Karton</td>
									<td class="center"><?php echo $dataProd['sisa_prod'];?> Cup</td>
									<td class="center"><?php echo $dataProd['tgl_persiapan'];?></td>
									<td class="center"><?php echo $statusPersiapan;?></td>
									<td class="center"><?php echo $dataProd['tgl_prod'];?></td>
									<td class="center"><?php echo $statusProduksi;?></td>
									<td class="center"><?php echo $dataProd['tgl_selesai'];?></td>
									<td class="center"><?php echo $statusSelesai;?></td>
									<td class="action"><a href="?pages=produksi&act=edit&id=<?php echo $dataProd['id_produksi'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=produksi&act=del&id=<?php echo $dataProd['id_produksi'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlProd2 = mysql_query("SELECT * FROM tb_produksi $addQuery ORDER BY id_produksi DESC");
							$jumlahData = mysql_num_rows($sqlProd2);

							$file="?pages=produksi&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_produksi = (int) filter($_GET['id']);

	$sqlProd = mysql_query("SELECT * FROM tb_produksi WHERE id_produksi=$id_produksi");
	$dataProd = mysql_fetch_array($sqlProd);

	$sqlRtu = mysql_query("SELECT id_rtu, jumlah as jumlah_rtu FROM tb_rtu WHERE id_prod='$id_produksi'");
	$dataRtu = mysql_fetch_array($sqlRtu);

	$sqlRts = mysql_query("SELECT id_rts, jumlah as jumlah_rts FROM tb_rts WHERE id_prod='$id_produksi'");
	$dataRts = mysql_fetch_array($sqlRts);

	$statusPersiapan = ($dataProd['status_persiapan']==1)? "checked":"";
	$statusProduksi = ($dataProd['status_prod']==1)? "checked":"";
	$statusSelesai = ($dataProd['status_selesai']==1)? "checked":"";	

	if(!empty($dataProd['status_prod']) && !empty($dataProd['tgl_prod']))	{
		$readonly = " readonly";
	}else{
		$readonly = " ";
	}

	if(!empty($dataProd['status_selesai']) && !empty($dataProd['tgl_selesai']))	{
		$readonly2 = " readonly";
	}else{
		$readonly2 = " ";
	}

	$sqlSisa = mysql_query("SELECT id_prod, jumlah_sisa FROM tb_sisa_produksi WHERE id_prod='$id_produksi'");
	$dataSisa = mysql_fetch_array($sqlRts);

	if(!empty($id_produksi)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Produksi </h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Produksi </h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<div style="float:left">
		<form method="POST" action="?pages=produksi&act=update">
		<input type="hidden" name="id_produksi" value="<?php echo $dataProd['id_produksi'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Kode Formula</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_formula" id="id_formula" placeholder="Kode Formula" required <?php echo $readonly;?>>
                      <option >- Pilih Kode Formula -</option>
                      
                      <?php
                            $sqlFormula = mysql_query("SELECT * FROM tb_formula");
                            while($dataFormula = mysql_fetch_array($sqlFormula)){
							 if($dataProd['id_formula']==$dataFormula['id_formula']){
						  		$selected = " selected ";
						  		$type = $dataFormula['type'];
						  		$id_formula = $dataFormula['id_formula'];
							  }else{
							  	$selected = "";
							  }
                      ?>
                      <option <?php echo $selected;?> value="<?php echo "$dataFormula[id_formula]";?>"><?php echo "$dataFormula[kode]";?></option>
                      <?php
                            }
                      ?>
                        </select>
                        <input type="hidden" value="<?php echo $id_formula;?>" name="id_formula" id="id_formula2">
                    </td>
			   	</tr>
			   	<tr>
					<td><label>Jumlah</label></td>
					<td>:</td>
					<td><input type="text" size="30" value="<?php echo "$dataProd[jumlah]";?>" name="jumlah" id="jumlah" placeholder="Jumlah Produksi" readonly> Karton</td>
			   	</tr>		   
			   	<tr>
					<td><label>Jumlah Per Karton</label></td>
					<td>:</td>
					<td><input type="text" size="30" value="<?php echo "$dataProd[jumlah_per_karton]";?>" name="jumlah_per_karton" id="jumlah_per_karton" placeholder="Jumlah Perkarton" readonly> Cup</td>
			   	</tr>
			   	<tr>
					<td><label>Jenis Formula</label></td>
					<td>:</td>
					<td><input type="text" size="30" value="<?php echo "$type";?>" name="type" id="type" placeholder="Jenis Formula" readonly></td>
			   	</tr>
			   	<tr>
					<td><label>Kode Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" size="30" value="<?php echo "$dataProd[kode_prod]";?>" name="kode_prod" id="kode_prod" placeholder="Kode Produksi" <?php echo $readonly;?>>						
					</td>
			   	</tr>			   
		   		<tr>
					<td><label>Sisa Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['sisa_prod'];?>" size="30" name="sisa_prod" id="sisa_prod" placeholder="Sisa Produksi" <?php echo $readonly2;?>> Cup
						<input type="hidden" name="id_sisa" value="<?php echo $dataSisa['id_sisa'];?>">
						<?php
						if($dataProd['status_selesai']==1 && !empty($dataProd['tgl_selesai']) && $dataProd['tgl_selesai']!='0000-00-00 00:00:00'){
							?>
							<input type="hidden" name="sisaAwal" value="<?php echo $dataProd['sisa_prod'];?>">
							<?php
						}else{
							?>
							<input type="hidden" name="sisaAwal" value="0">
							<?php
						}
						?>
					</td>
			   	</tr>			   				   			   				   				   
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>

		</div>
		<div style="float:left;margin-left:20px">
	
			<input type="hidden" name="id_rtu" value="<?php echo $dataRtu['id_rtu'];?>">
			<input type="hidden" name="id_rts" value="<?php echo $dataRts['id_rts'];?>">
			<input type="hidden" name="jumlah_rtu" value="<?php echo $dataRtu['jumlah_rtu'];?>">
			<input type="hidden" name="jumlah_rts" value="<?php echo $dataRts['jumlah_rts'];?>">
			<table style="width:auto">
				<tr>
					<td><label>Tgl. Persiapan</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['tgl_persiapan'];?>" <?php echo $readonly;?> size="30" name="tgl_persiapan" id="tgl_persiapan" placeholder="Tgl. Persiapan" required>						
					</td>
			   	</tr>
			   	<tr>
			   	  <td><label>Status Persiapan</label></td>
			   	  <td>:</td>
			   	  <td>
			   	  	<input type="checkbox" name="status_persiapan" id="status_persiapan" <?php echo $readonly;?>  <?php echo $statusPersiapan;?>>			   	  	
			   	  </td>
		   	  </tr>	
		   	  	<tr>
					<td><label>Tgl. Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['tgl_prod'];?>" size="30" name="tgl_prod" id="tgl_prod" placeholder="Tgl. Produksi" <?php echo $readonly2;?> >						
					</td>
			   	</tr>
			   	<tr>
			   	  <td><label>Status Produksi</label></td>
			   	  <td>:</td>
			   	  <td>
			   	  	<input type="checkbox" name="status_prod" id="status_prod" <?php echo $readonly2;?>  <?php echo $statusProduksi;?>>			   	  	
			   	  </td>
		   	  </tr>	
		   	  	<tr>
					<td><label>Tgl. Selesai</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['tgl_selesai'];?>" size="30" name="tgl_selesai" id="tgl_selesai" placeholder="Tgl. Selesai" >						
					</td>
			   	</tr>
			   	<tr>
			   	  <td><label>Status Selesai</label></td>
			   	  <td>:</td>
			   	  <td>
			   	  	<input type="checkbox" name="status_selesai" id="status_selesai" <?php echo $statusSelesai;?>>			   	  	
			   	  </td>
		   	  </tr>	
			  <tr>			 		
			 		<td colspan="3" align="right">
						<input type="button" value="Cetak Bahan Baku" name="cetak" id="cetak" class="btn btn-warning">
					</td>
		   		</tr>
			</table>
		</form>
		</div>
	</div>


<?php
}elseif ($act=='update') {
	# code...
	$id_produksi = (int) $_POST['id_produksi'];
	$id_formula = (int) $_POST['id_formula'];
	$kode_prod = $_POST['kode_prod'];
	$jumlah = (int) $_POST['jumlah'];
	$jumlah_per_karton = (int) $_POST['jumlah_per_karton'];
	$sisa_prod = (int) $_POST['sisa_prod'];
	$tgl_persiapan = $_POST['tgl_persiapan'];
	$status_persiapan = $_POST['status_persiapan'];
	$tgl_prod = $_POST['tgl_prod'];
	$status_prod = $_POST['status_prod'];
	$tgl_selesai = $_POST['tgl_selesai'];
	$status_selesai = $_POST['status_selesai'];
	$id_rts = $_POST['id_rts'];
	$id_rtu = $_POST['id_rtu'];	
	$jumlah_rts = $_POST['jumlah_rts'];
	$jumlah_rtu = $_POST['jumlah_rtu'];
	$type = $_POST['type'];
	$id_sisa = (int) $_POST['id_sisa'];
	$sisaAwal = (int) $_POST['sisaAwal'];

	$status_persiapan = (isset($status_persiapan))? '1':'0';
	$status_prod = (isset($status_prod))? '1':'0';
	$status_selesai = (isset($status_selesai))? '1':'0';
	
	$sqlFormula = mysql_query("SELECT kode_bb, jumlah FROM tb_formula_detail WHERE id_formula='$id_formula'");
	while ($dataFormula = mysql_fetch_array($sqlFormula)) {
		$kode_bb = $dataFormula['kode_bb'];
		$jumlahFormula = $dataFormula['jumlah'];

		$jumlahBB = mysql_fetch_array(mysql_query("SELECT stock_bb, limit_bb, nama FROM tb_kode_bb WHERE kode='$kode_bb'"));		

		$selisih = $jumlahBB['stock_bb'] - $jumlahFormula;

		if($selisih<=$jumlahBB['limit_bb']){
			sukses("Stock B.Baku $jumlahBB[nama] tidak Mecukupi", '?pages=produksi&act=edit');
			exit;
		}
	}

	if(!empty($id_produksi)){

		$cekKodeProd = mysql_query("SELECT id_produksi FROM tb_produksi WHERE kode_prod='$kode_prod' AND id_produksi!='$id_produksi'");
		$onoKode = mysql_num_rows($cekKodeProd);

		if($onoKode){
			sukses('Kode Produksi sudah ada ', '?pages=produksi&act=edit');
			exit;
		}

		$cekFormula = mysql_fetch_array(mysql_query("SELECT id_formula FROM tb_produksi WHERE id_produksi = '$id_produksi' LIMIT 1"));
		if($cekFormula['id_formula']!=$id_formula){
			$delBBKeluar = mysql_query("DELETE FROM tb_bb_keluar WHERE id_prod='$id_produksi'");

			$sqlFormula = mysql_query("SELECT kode_bb, jumlah, satuan FROM tb_formula_detail WHERE id_formula='$id_formula'");
			while ($dataFormula = mysql_fetch_array($sqlFormula)) {
				$kode_bb = $dataFormula['kode_bb'];
				$jumlahFormula = $dataFormula['jumlah'];
				$satuanFormula = $dataFormula['satuan'];

				$insertKeluar = mysql_query("INSERT INTO tb_bb_keluar VALUES(NULL, '', '$jumlahFormula', '$satuanFormula', '$id_produksi', '$kode_bb', '0','0')");
			}	
		}

		$update = mysql_query("UPDATE tb_produksi SET kode_prod='$kode_prod', sisa_prod='$sisa_prod', tgl_persiapan='$tgl_persiapan', jumlah_per_karton='$jumlah_per_karton', tgl_prod='$tgl_prod', tgl_selesai='$tgl_selesai', jumlah='$jumlah', status_persiapan='$status_persiapan', status_prod='$status_prod', status_selesai='$status_selesai', id_formula='$id_formula' WHERE id_produksi='$id_produksi'");		

		if($status_selesai==1 && !empty($tgl_selesai) && $tgl_selesai!='0000-00-00 00:00:00'){

			if(!empty($sisa_prod) && !empty($id_sisa)){
				$dataStockSisa = mysql_fetch_array(mysql_query("SELECT stock_sisa FROM tb_temp_rts LIMIT 1"));
				$stock_sisa = $dataStockSisa['stock_sisa'];
				$jumlahSisa = ($stock_sisa - $sisaAwal)+$sisa_prod;

				$updateStockSisa = mysql_query("UPDATE tb_temp_rts SET stock_sisa='$jumlahSisa'");	

				$updateSisa = mysql_query("UPDATE tb_sisa_produksi SET jumlah_sisa='$sisa_prod' WHERE id_sisa='$id_sisa'");
			}else {
				$insertSisa = mysql_query("INSERT INTO tb_sisa_produksi VALUES(NULL, '$id_produksi', '$sisa_prod')");
				
				$dataStockSisa = mysql_fetch_array(mysql_query("SELECT stock_sisa FROM tb_temp_rts LIMIT 1"));
				$stock_sisa = $dataStockSisa['stock_sisa'];
				$jumlahSisa = $stock_sisa + $sisa_prod;

				$updateStockSisa = mysql_query("UPDATE tb_temp_rts SET stock_sisa='$jumlahSisa'");				
			}

			if($type=="RTU"){				

				if(!empty($id_rtu)){
					$updateRtu = mysql_query("UPDATE tb_rtu SET jumlah='$jumlah' WHERE id_rtu='$id_rtu' AND id_prod='$id_produksi'");
					/*
					$dataStockRtu = mysql_fetch_array(mysql_query("SELECT stock_rtu FROM tb_temp_rts LIMIT 1"));
					$stock_rtu = $dataStockRtu['stock_rtu'];
					$jumlahRtu = ($stock_rtu + $jumlah_rtu) - $jumlah;

					$updateStokRtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu='$jumlahRtu'");
					*/
				}else{
					$insertRtu = mysql_query("INSERT INTO tb_rtu VALUES(NULL, '$id_produksi', '$jumlah', 'Karton', '0', '')");		
					/*
					$dataStockRtu = mysql_fetch_array(mysql_query("SELECT stock_rtu FROM tb_temp_rts LIMIT 1"));
					$stock_rtu = $dataStockRtu['stock_rtu'];
					$jumlahRtu = ($stock_rtu + $jumlah);

					$updateStokRtu = mysql_query("UPDATE tb_temp_rts SET stock_rtu='$jumlahRtu'");		
					*/			
				}
			}elseif($type=="RTS"){
				if(!empty($id_rts)){
					$updateRts = mysql_query("UPDATE tb_rts SET jumlah='$jumlah' WHERE id_rts='$id_rts' AND id_prod='$id_produksi'");

					/*$dataStockRts = mysql_fetch_array(mysql_query("SELECT stock_rts FROM tb_temp_rts LIMIT 1"));
					$stock_rts = $dataStockRts['stock_rts'];
					$jumlahRts = ($stock_rts + $jumlah_rts) - $jumlah;

					$updateRts = mysql_query("UPDATE tb_temp_rts SET stock_rts='$jumlahRts'");
					*/
				}else{
					$insertRts = mysql_query("INSERT INTO tb_rts VALUES(NULL, '$id_produksi', '$jumlah', 'Karton', '0', '', '0','0')");
					/*
					$dataStockRts = mysql_fetch_array(mysql_query("SELECT stock_rts FROM tb_temp_rts LIMIT 1"));
					$stock_rts = $dataStockRts['stock_rts'];
					$jumlahRts = $stock_rts +  $jumlah;

					$updateRts = mysql_query("UPDATE tb_temp_rts SET stock_rts='$jumlahRts'");
					*/
				}
			}
		}
		
		if($update)
			sukses('Data berhasil disimpan', '?pages=produksi');
		else
			sukses('Data gagal disimpan', '?pages=produksi');
			
	}else{

		$cekKodeProd = mysql_query("SELECT id_produksi FROM tb_produksi WHERE kode_prod='$kode_prod'");
		$onoKode = mysql_num_rows($cekKodeProd);

		if($onoKode){
			sukses('Kode Produksi sudah ada ', '?pages=produksi&act=edit');
			exit;
		}

		$insert = mysql_query("INSERT INTO tb_produksi VALUES(NULL, '$kode_prod', '$tgl_persiapan', '$tgl_prod', '$tgl_selesai', '$jumlah', '$jumlah_per_karton', '$sisa_prod', '$status_persiapan', '$status_prod', '$status_selesai', '$id_formula')");
		
		$currentProd = mysql_fetch_array(mysql_query("SELECT id_produksi FROM tb_produksi ORDER BY id_produksi DESC LIMIT 1"));
		$curId = $currentProd['id_produksi'];	
		$sqlFormula = mysql_query("SELECT kode_bb, jumlah, satuan FROM tb_formula_detail WHERE id_formula='$id_formula'");
		while ($dataFormula = mysql_fetch_array($sqlFormula)) {
			$kode_bb = $dataFormula['kode_bb'];
			$jumlahFormula = $dataFormula['jumlah'];
			$satuanFormula = $dataFormula['satuan'];

			$insertKeluar = mysql_query("INSERT INTO tb_bb_keluar VALUES(NULL, '', '$jumlahFormula', '$satuanFormula', '$curId', '$kode_bb', '0','0')");
		}		
		
		if($insert)
			sukses('Data berhasil disimpan', '?pages=produksi');
		else
			sukses('Data gagal disimpan', '?pages=produksi');
	}
	
}elseif ($act=='del') {
	# code...
	$id_produksi = (int) filter($_GET['id']);

	$dataProd = mysql_fetch_array(mysql_query("SELECT * FROM tb_produksi WHERE id_produksi='$id_produksi'"));

	$sqlFormula = mysql_query("SELECT b.id_bb_keluar, a.kode_bb, a.jumlah FROM tb_formula_detail a, tb_bb_keluar b WHERE id_formula='$dataProd[id_formula]' AND a.kode_bb=b.kode_bb AND b.status=1 AND b.id_prod='$id_produksi'");

	while ($dataFormula = mysql_fetch_array($sqlFormula)) {
		$kode_bb = $dataFormula['kode_bb'];
		$jumlahFormula = $dataFormula['jumlah'];
		$id_bb_keluar = $dataFormula['id_bb_keluar'];

		$dataKodeBB = mysql_fetch_array(mysql_query("SELECT stock_bb FROM tb_kode_bb WHERE kode='$kode_bb'"));
		$jumlahAwal = $dataKodeBB['stock_bb'];
		$jumlahAkhir = $jumlahAwal + $jumlahFormula;		

		$updateKodeBB = mysql_query("UPDATE tb_kode_bb SET stock_bb='$jumlahAkhir' WHERE kode='$kode_bb'");	

		$delBBKeluar = mysql_query("DELETE FROM tb_bb_keluar WHERE id_bb_keluar='$id_bb_keluar'");
	}	
	
	$dataSisa = mysql_fetch_array(mysql_query("SELECT jumlah_sisa FROM tb_sisa_produksi WHERE id_prod='$id_produksi'"));
	$jumlah_sisa = $dataSisa['jumlah_sisa'];
	
	$dataStockSisa = mysql_fetch_array(mysql_query("SELECT stock_sisa FROM tb_temp_rts LIMIT 1"));
	$stock_sisa = $dataStockSisa['stock_sisa'];
	$jumlahSisa = $stock_sisa - $jumlah_sisa;

	$updateStockSisa = mysql_query("UPDATE tb_temp_rts SET stock_sisa='$jumlahSisa'");
	$deleteSisa = mysql_query("DELETE FROM tb_sisa_produksi WHERE id_prod='$id_produksi'");
	
	$delete = mysql_query("DELETE FROM tb_produksi WHERE id_produksi='$id_produksi'");
	
	$deleteRtu = mysql_query("DELETE FROM tb_rtu WHERE id_prod='$id_produksi'");
	$deleteRts = mysql_query("DELETE FROM tb_rts WHERE id_prod='$id_produksi'");
	
	if($delete)
		sukses('Data berhasil dihapus', '?pages=produksi');
}
?>					